package com.example.back.service;

import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class ComponentValidationService {
    
    // 定义组件之间的连接规则
    private final Map<String, Set<String>> validConnections = new HashMap<>();
    
    public ComponentValidationService() {
        // 初始化连接规则
        // 邻接矩阵可以连接到图算法
        validConnections.put("adjacencyMatrix", new HashSet<>(Arrays.asList("dijkstra", "bfs", "dfs", "prim", "kruskal")));
        
        // 邻接表可以连接到图算法
        validConnections.put("adjacencyList", new HashSet<>(Arrays.asList("dijkstra", "bfs", "dfs", "prim", "kruskal")));
        
        // 数组可以连接到排序和查找算法
        validConnections.put("array", new HashSet<>(Arrays.asList("bubbleSort", "quickSort", "mergeSort", "binarySearch", "heapSort", "insertionSort", "selectionSort")));
        
        // 二叉树可以连接到树遍历算法
        validConnections.put("binaryTree", new HashSet<>(Arrays.asList("bfs", "dfs")));
        
        // 堆可以连接到排序算法
        validConnections.put("heap", new HashSet<>(Arrays.asList("bubbleSort", "quickSort", "mergeSort")));
    }
    
    public boolean canConnect(String sourceType, String targetType) {
        // 添加日志帮助调试
        System.out.println("验证连接: " + sourceType + " -> " + targetType);
        System.out.println("有效目标: " + validConnections.get(sourceType));
        
        // 检查源组件是否可以连接到目标组件
        Set<String> validTargets = validConnections.get(sourceType);
        return validTargets != null && validTargets.contains(targetType);
    }
} 